<?php namespace App\Http\Controllers\Admin;

use App\Category;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Product;
use Request,Response,Redirect;

class ProductController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$products = Product::with('category')->paginate(8);
		return view('admin.products.index', compact('products'));
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		$categories_all = Category::all();
		$categories = [];
		foreach($categories_all as $category) {
			$categories[$category->id] = $category->title;
		}

		return view('admin.products.create', compact('categories'));
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$product = new Product;
		$title = Request::input('title');
		$category_id = Request::input('category_id');
		$image = Request::file('image');
		$description = Request::input('description');
		if (Request::hasFile('image'))
		{
			$file_name = 'product-'.time().'.'.$image->getClientOriginalExtension();
			$uploadSuccess = $image->move(public_path().'/images', $file_name);
			$file_path = 'images/'.$file_name;
			if($uploadSuccess) {
				$product->title = $title;
				$product->category_id = $category_id;
				$product->image_url = $file_path;
				$product->description = $description;
				$product->save();

				return Redirect::route('admin.product.index')->with('message', '上传成功');
			}
		}

		return Response::json(['message' => 'upload error', 'status' => 'error', 'statusCode' => 500]);
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$product = Product::find($id);
		$categories_all = Category::all();
		$categories = [];
		foreach($categories_all as $category) {
			$categories[$category->id] = $category->title;
		}

		return view('admin.products.create', compact('categories', 'product'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$product = Product::find($id);
		$title = Request::input('title');
		$category_id = Request::input('category_id');
		$image = Request::file('image');
		$description = Request::input('description');
		if (Request::hasFile('image'))
		{
			$file_name = 'product-'.time().'.'.$image->getClientOriginalExtension();
			$uploadSuccess = $image->move(public_path().'/images', $file_name);
			$file_path = 'images/'.$file_name;
			if($uploadSuccess) {
				$product->title = $title;
				$product->category_id = $category_id;
				$product->image_url = $file_path;
				$product->description = $description;
				$product->update();

				return Redirect::route('admin.product.index')->with('message', '上传成功');
			}
		}

		return Response::json(['message' => 'upload error', 'status' => 'error', 'statusCode' => 500]);
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
